#!/usr/bin/env perl

use DBI;
#use strict;
use Getopt::Long;#Std;
our($debug, $verbose, $help, $show_dep, $backup);
our($calendar);

require "QSQLdata.pl";

# h  #
# e  #
# l  # Command line options processing
# p  #
sub init()
{
    GetOptions( 'd|debug' => \$debug,
                'v|verbose' => \$verbose,
                'h|help' => \$help,
                's|show_dep' => \$show_dep,
                'b|backup' => \$backup,
                'c|calendar' => \$calendar ) or usage();
    system("mkdir -p Annotator-tmp");
    system("mkdir -p BackUp");

    print "main".$frurl;

    if ($debug) {
            print " \$debug equals to $debug \n";
            print " \$verbose equals to $verbose \n";
            print " \$help equals to $help \n";
            print " \$show_dep equals to $show_dep \n";
            print " \$backup equals to $backup \n";
            print " \$calendar equals to $calendar \n";
    }
    usage() if $help;
    show_dep() if $show_dep;
    calendar() if $calendar;
    backup() if $backup;
}
#
# Message about this program and how to use it
#
sub usage()
{
    print STDERR << "EOF" ;

    usage: $0 [-hsvdbc]
     -h              : this (help) message
     --help          : this (help) message
     -s              : show package dependencies
     --show_dep      : show package dependencies
     -v              : verbose output
     --verbose       : verbose output
     -d              : print debugging messages to stderr
     --debug         : print debugging messages to stderr
     -b              : Make a vcf backup of your rubric, including emails
     --backup        : Make a vcf backup of your rubric, including emails
     -c              : Add to your calendar the contents of
~/.mozilla/firefox/xywqpdeu.default/reminderfox/reminderfox.ics file
     --calendar      : Add to your calendar the contents of 
~/.mozilla/firefox/xywqpdeu.default/reminderfox/reminderfox.ics file

    Edit QSQLdata.pl in order to tune the present script!

EOF
    exit;
}

#
# Message about this program dependencies
#
sub show_dep()
{
    print STDERR << "EOF" ;

    Dependencies:

    -using FreeBSD: 
      p5-DBI > 1.615
      p5-DBD-SQLite > 1.31

    -using Debian: 
      libdbd-sqlite3-perl 
      libtie-ical-perl 
      libdate-ical-perl 
      perl-doc

EOF
    exit;
}

#
# Backup
#
sub backup()
{
    print STDERR << "EOF" ;

    BACKUP!

    This funcion assumes that your QtMoko mobile is connected via USB 
    and reachable as \$frurl. 

    The file qtopia_db.sqlite will be copied in ./BackUp/ 
    as qtopia_db`date "+%Y-%m-%d"`.sqlite and converted into a vcard file 
    named qtopia_db`date "+%Y-%m-%d"`.vcf in the same directory. 

    The total number of your contacts will than be shown. 

EOF
    system('scp $frurl:/home/root/Applications/Qtopia/qtopia_db.sqlite BackUp/qtopia_db`date "+%Y-%m-%d"`.sqlite');
    system('./sql2vcf.pl -i BackUp/qtopia_db`date "+%Y-%m-%d"`.sqlite -o BackUp/qtopia_db`date "+%Y-%m-%d"`.vcf');
    system('grep FN BackUp/qtopia_db`date "+%Y-%m-%d"`.vcf | sort > BackUp/contact_list_`date "+%Y-%m-%d"`.vcf');
    exit;
}

#
# Calendar
#
sub calendar()
{
    print STDERR << "EOF" ;

    CALENDAR!

    This function assumes that your QtMoko mobile is connected via USB 
    and reachable as \$frurl. 

    The file ~/.mozilla/firefox/xywqpdeu.default/reminderfox/reminderfox.ics, 
    which is the one used by Reminderfox (a Firefox plug-in I use) is used to:
    - update the conten of qtopia_db.sqlite    
      (in \$frurl:/home/root/Applications/Qtopia/);
    - update the content of Annotator/
      (in \$frurl:/home/root/Applications/).

EOF
    my $calename = '~/.mozilla/firefox/xywqpdeu.default/reminderfox/reminderfox.ics';
    system('scp $frurl:/home/root/Applications/Qtopia/qtopia_db.sqlite BackUp/qtopia_db`date "+%Y-%m-%d"`.sqlite');
    system('cp BackUp/qtopia_db`date "+%Y-%m-%d"`.sqlite qtopia_db.sqlite');
    system('perl deleteqtcalappointments.pl qtopia_db.sqlite');
    #my prolix = "-v" if $verbose;
    system('grep -v X-MOZ-LASTACK $calename > $calename.tmp');
    system('sed -e "s/\r$//g" $calename.tmp >$calename-2.tmp ');
    system('perl ics2qtcal.pl $verbose --ical $calename-2.tmp --qtopiadb qtopia_db.sqlite --notesdirectory ./Annotator-tmp ');
    system('rm $calename*.tmp ');
    system('scp qtopia_db.sqlite $frurl:/home/root/Applications/Qtopia/ ');
    system('ssh $frurl "rm -f /home/root/Applications/Annotator/0-*" ');
    system('scp -q Annotator-tmp/* $frurl:/home/root/Applications/Annotator/ ');
    exit;
}

# Help messages or init variables
&init();

